package parroty;

import java.util.Vector;
import com.google.wave.api.*;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class ParrotyServlet extends AbstractRobotServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8804856258611272880L;

	Vector<Language> selectedLanguage = new Vector<Language>();
	Vector<Language> supported = new Vector<Language>();

	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
		supported(); 

		if (bundle.wasSelfAdded()) {
			Blip blip = wavelet.appendBlip();
			TextView textView = blip.getDocument();

			textView
					.append("@ hi I'll be your translator today");

		}

		for (Event e : bundle.getEvents()) {

			if (e.getType() == EventType.BLIP_SUBMITTED) {
				if (String.valueOf(
						e.getBlip().getDocument().getText().charAt(0))
						.startsWith("@")
						|| String.valueOf(
								e.getBlip().getDocument().getText().charAt(0))
								.startsWith("%")
						 == false) {

					Blip blip = wavelet.appendBlip();
					// Blip waveTheme = wavelet.getRootBlip();
					translate(blip, e);

					// inicio escolha linguagem

				} else if (String.valueOf(e.getBlip().getDocument().getText())
						.startsWith("%")) {
					languagesSet(e);
				}
			}
			if (e.getType() == EventType.WAVELET_PARTICIPANTS_CHANGED){
				Blip blip = wavelet.appendBlip();
				TextView textView = blip.getDocument();

				textView
						.append("@ set your main language %[code] ex: %pt , one per blip ,supported list :");
						for(int x =0;x<supported.size();x++)
						{
							blip.createChild().getDocument().append(supported.get(x).name()+" code: "+supported.get(x).toString()+" ,");
						}
						
			}
		}
	}
	
	public void languagesSet(Event e) {
		for(int x=0;x<supported.size();x++){
		if (supported.get(x).toString().equals(String.valueOf(e.getBlip().getDocument().getText()).substring(1)
				)){
			if(!selectedLanguage.contains(supported.get(x))){
			selectedLanguage.add(supported.get(x));
			}
		}
		}
		// fim escolha linguagem

	}
	public void supported(){
		supported.clear();
		supported.add(Language.PORTUGUESE);
		supported.add(Language.CHINESE_SIMPLIFIED);
		supported.add(Language.ENGLISH);
		supported.add(Language.FRENCH);
		supported.add(Language.ITALIAN);
		supported.add(Language.JAPANESE);
		supported.add(Language.GERMAN);
		supported.add(Language.GREEK);
		supported.add(Language.HINDI);
		supported.add(Language.HEBREW);
		supported.add(Language.SWEDISH);
	}
	public void translate(Blip blip, Event e) {
		for (int x = 0; x < selectedLanguage.size(); x++) {
			Translate.setHttpReferrer("http://www.google.com.br");
			String translatedText;
			
			try {
				translatedText = Translate.execute(e.getBlip().getDocument()
						.getText(), Language.AUTO_DETECT, selectedLanguage
						.get(x));
				blip.createChild().getDocument().append("@"+selectedLanguage.get(x).name() +" : "+ translatedText);

			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();

			}
			
		}
	}

	public static void main(String[] args) {
		// TODO code application logic here
		
		System.out.print("lingua: "+Language.FRENCH);
		
	}
}
